from flask import request, jsonify
from src.api.controllers.copywriting_controller import CopywritingController
from src.utils.logger import log_info

def register_copywriting_routes(bp):
    controller = CopywritingController()
    
    @bp.route('/copywriting/process_folder', methods=['POST'])
    def process_videos_in_folder():
        """处理文件夹中的视频文件，提取文本"""
        folder_path = request.json.get('folder_path')
        api_key = request.json.get('api_key')
        log_info(f"接收到处理文件夹中视频的请求: 路径={folder_path}")
        return controller.process_videos_in_folder(folder_path, api_key)
    
    @bp.route('/copywriting/default_prompt', methods=['GET'])
    def get_default_prompt():
        """获取默认文案生成提示词"""
        log_info("接收到获取默认提示词请求")
        return controller.get_default_prompt()
    
    @bp.route('/copywriting/generate', methods=['POST'])
    def generate_copywriting():
        """生成文案"""
        transcript = request.json.get('transcript')
        api_key = request.json.get('api_key')
        custom_prompt = request.json.get('custom_prompt')
        return controller.generate_copywriting(transcript, api_key, custom_prompt)
    
    @bp.route('/copywriting/generate_batch', methods=['POST'])
    def generate_copywritings_batch():
        """批量生成文案"""
        transcripts = request.json.get('transcripts')
        api_key = request.json.get('api_key')
        custom_prompt = request.json.get('custom_prompt')
        log_info(f"接收到批量生成文案请求: 文本数量={len(transcripts) if transcripts else 0}")
        return controller.generate_copywritings_batch(transcripts, api_key, custom_prompt)
    
    @bp.route('/copywriting/export_excel', methods=['POST'])
    def export_to_excel():
        """导出文案到Excel文件"""
        data_list = request.json.get('data')
        log_info(f"接收到导出Excel请求: 数据条数={len(data_list) if data_list else 0}")
        return controller.export_to_excel(data_list)